package top.lgd.controller.operation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lgd.base.LgdConstants;
import top.lgd.base.LgdResult;
import top.lgd.base.LgdTable;
import top.lgd.controller.LgdController;
import top.lgd.pojo.entity.operation.Member;
import top.lgd.pojo.req.operation.MemberOpReq;
import top.lgd.service.operation.MemberService;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/19 19:12
 */
@RestController
@RequestMapping("/admin/operationMember")
public class MemberAdminController extends LgdController {

    @Autowired
    private MemberService memberService;

    /**
     * 查询会员列表
     *
     * @return
     */
    @PostMapping("/queryList")
    public LgdTable<Member> queryList(@RequestBody Member member) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper(member);
        queryWrapper.eq(LgdConstants.COLUMN_DATA_USER_ID, current().getId())
                .orderByDesc(LgdConstants.COLUMN_ID);
        Page<Member> page = memberService.page(new Page(member.getCurrent(), member.getPageSize()), queryWrapper);
        return getTable(page);
    }

    /**
     * 添加编辑会员
     *
     * @param member 会员 对象
     * @return
     */
    @PostMapping("addUpdate")
    public LgdResult<Member> addUpdate(@RequestBody Member member) {
        member.setDataUserId(current().getId());
        if (memberService.addUpdateAndFlow(member)) {
            return LgdResult.success(member);
        } else {
            return LgdResult.fail("添加或编辑会员 失败");
        }
    }

    /**
     * 更新 会员
     *
     * @param memberOpReq 会员 对象
     * @return
     */
    @PostMapping("memberOp")
    public LgdResult<Member> memberOp(@RequestBody MemberOpReq memberOpReq) {
        Member member = memberService.queryMember(memberOpReq.getId());
        if (null == member) {
            return LgdResult.fail("会员不存在");
        }
        if (memberService.opAndFlow(member, memberOpReq)) {
            return LgdResult.success(member);
        } else {
            return LgdResult.fail("更新会员 失败");
        }
    }
}